package com.xhui.vimarket.zkplus.zul.attr.mod.impl;t java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Span;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Timebox;
import org.zkoss.zul.Toolbarbutton;

import com.xhui.crm.common.Constants;
import com.xhui.crm.common.ResConstants;
import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.util.com.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusil;
import com.xhui.vimarkcom.xhui.vimarket.zkplusttr.AttrExt;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.AttrInst;
import comcom.xhui.vimarket.zkpluszkplus.zul.attr.bean.AttrTmpl;com.xhui.vimarket.zkplusi.vimarket.zkplus.zul.attr.bean.ValiVo;
com.xhui.vimarket.zkplus.vimarket.zkplus.zul.attr.mod.AttrCocom.xhui.vimarket.zkplusmport com.xhui.vimarket.zkplus.zul.attr.mocom.xhui.vimarket.zkplus
import com.xhui.vimarket.zkplus.zul.atcom.xhui.vimarket.zkplusueFromRule;
import com.xhui.vimarket.com.xhui.vimarket.zkplus.rule.AttrValueToRule;
import com.xhui.vimarket.zkplus.zul.attr.utcom.xhui.vimarket.zkpluss;
import com.xhui.vimarket.zkplus.zul.attr.util.AttrUtil;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrDateComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrTimeComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrTimeComp inst   = new AttrTimeComp();
    
    /**
     * 日志.
     */
    private static Logger       logger = Logger.getLogger(AttrTimeComp.class);
    
    /**
     * 构造函数.
     */
    private AttrTimeComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrTimeComp instance() {
        return AttrTimeComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @SuppressWarnings("deprecation")
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // panel
        final Hbox hbox = new Hbox();
        hbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        hbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        hbox.setAttribute(AttrBaseComp.COMP_CANTNULL, AttrCompUtil.getStrCantNull(attrTmpl
            .isCantNull()));
        hbox.setAttribute(AttrBaseComp.COMP_JAVACODE, attrTmpl.getJavaCode());
        hbox.setAttribute(AttrBaseComp.COMP_COMP_NAME, attrTmpl.getName());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_LENGTH, attrTmpl.getValueLength());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_FROM, attrTmpl.getValueFrom());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_TO, attrTmpl.getValueTo());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_REX, attrTmpl.getValidExpr());
        hbox.setWidth("100%");
        hbox.setWidths(this.getWidthsLCT(attrTmpl));
        final Div labDiv = new Div();
        final Label label = new Label(attrTmpl.getName());
        label.setSclass("zp-lt-label");
        /*
        final String sTimeboxWithComposer = attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
        final Timebox timebox = (Timebox) Executions.createComponentsDirectly(sTimeboxWithComposer,
            null, null, null);
            */
        final Timebox timebox = (Timebox) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, null);
        
        final Div boxDiv = new Div();
        if (attrExt.getAttrAda().isViewMode()) { // 展示模式
            timebox.setReadonly(true);
        }
        timebox.setFormat("HH:mm:ss");
        timebox.setWidth(AttrBaseComp.WIDTH_SELECT);
        //        final String sToolbarbuttonWithComposer = attrExt.getAttrAda().getAttrResetStr(
        //            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
        //        final Toolbarbutton renullBtn = (Toolbarbutton) Executions.createComponentsDirectly(
        //            sToolbarbuttonWithComposer, null, null, null);
        final Toolbarbutton renullBtn = (Toolbarbutton) attrExt.getAttrAda().getAttrResetStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, null);
        renullBtn.setTooltiptext("清空已设置值");
        renullBtn.setImage(ResConstants.IMG_RESET);
        if (attrTmpl.getHint() != null) {
            hbox.setTooltiptext(attrTmpl.getHint());
        }
        if (!attrTmpl.isDisplay()
            || AttrConstants.DISPLAY_STYLE_PANEL_HIDE.equals(attrTmpl.getDisplayStyle())) {
            hbox.setVisible(false); // 隐藏
        }
        // info
        final Span infDiv = this.createRulebar(attrExt, hbox, attrTmpl);
        // add to zul
        hbox.setParent(comp);
        labDiv.setParent(hbox);
        label.setParent(labDiv);
        infDiv.setParent(labDiv);
        boxDiv.setParent(hbox);
        timebox.setParent(boxDiv);
        renullBtn.setParent(boxDiv);
        // toolbox
        this.createToolbox(hbox, attrExt.isToolboxVisible(), attrTmpl);
        // comp log
        if (AttrTimeComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<hbox width=\"100%\" widths=\"40%,60%\">");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerComp("<label value=\"");
            attrExt.appendInnerComp(attrTmpl.getName());
            attrExt.appendInnerComp("\" />");
            attrExt.appendInnerComp("<timebox format=\"yyyy-MM-dd\" />");
            attrExt.appendInnerComp("</hbox>");
        }
        // init default value
        if ("systime".equals(ZkUiUtil.getUrlParam(attrTmpl.getParam1(), "default"))) {
            // Date today = DateUtil.getSaveDate();
            // timebox.setValue(today);
            // timebox.setText(DateUtil.convertDateToString(today));
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList, final Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList, attrViewMap);
        // 取输入框值
        final Component compTimebox = getTimebox(comp);
        if (compTimebox != null) {
            final long theAttrId = NumUtils
                .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
            final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                theAttrId);
            if (theAttrList.size() > 0) {
                ((Timebox) compTimebox).setText(theAttrList.get(0).getParam1());
                // javacode回填
                AttrCompUtil.copyTmplInfo(theAttrList.get(0), comp);
            }
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 取输入框值
        final Component compTimebox = getTimebox(comp);
        if (compTimebox != null) {
            // Date date = ((Timebox) compTimebox).getText();
            if (!"".equals(((Timebox) compTimebox).getText())) { // 有填写
                final AttrInst attrInst = attrExt.newAttrInst();
                attrInst.setAttrId(NumUtils
                    .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
                attrInst
                    .setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
                attrInst.setParam1(((Timebox) compTimebox).getText());
                attrInst.setOp(this.getToolbarOp(comp));
                retLicom.xhui.vimarket.zkplus);
            }
        }
        return com.xhui.vimarket.zkplus
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        if (AttrCompUtil.isCantNull(comp)) { // 必填
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) {
                ValiVo retVo = new ValiVo();
                ArrayList<ValiVo> retVoList = new ArrayList<ValiVo>();
                retVoList.add(retVo);
                return retVoList;
            }
        }
        return null;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        final AttrInst attrInst = attrExt.newAttrInst();
        attrInst.setAttrId(attrTmpl.getAttrId());
        attrInst.setJavaCode(attrTmpl.getJavaCode());
        retList.add(attrInst);
        return retList;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        super.clean(attrExt, comp);
        // 取输入框值
        final Component compTimebox = getTimebox(comp);
        if (compTimebox != null) {
            ((Timebox) compTimebox).setText("");
        }
    }
    
    /**
     * getTextbox.
     * 
     * @param root Component
     * @return Timebox
     */
    public static Timebox getTimebox(Component root) {
        // 取输入框
        // hbox // Timebox(1) 
        return (Timebox) root.getFirstChild().getNextSibling().getFirstChild();
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
        super.setDisabled(attrExt, comp, tflag);
        ZkUiUtil.setDisabled(comp, tflag);
    }
    
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
        if (valList == null || valList.size() < 1) { // 没有值
            if (AttrCompUtil.isCantNull(comp)) { // 需限制不可空
                return AttrCompUtil.getRetVoList(AttrCompConstants.RET_ERROR, AttrCompUtil
                    .getCompName(comp)
                    + AttrCompConstants.STRING_CANT_NULL, comp);
            }
            return null; // 通过
        } else { // 其他规则验证
            RetVO fromRetVo = AttrValueFromRule.judgeTime(valList, comp);
            if (!Constants.RET_TRUE.equals(fromRetVo.getResult())) {
                return AttrUtil.tranRetVoToOneList(fromRetVo);
            }
            RetVO toRetVo = AttrValueToRule.judgeTime(valList, comp);
            if (!Constants.RET_TRUE.equals(toRetVo.getResult())) {
                return AttrUtil.tranRetVoToOneList(toRetVo);
            }
        }
        return null; // 通过
    }
    
    /**
     * 重置.
     * 
     * @param comp Component
     * @author wuyx
     * 2011-5-12 wuyx 
     */
    public static void reset(Component comp) {
        getTimebox(comp).setText("");
    }
    
    /**
     * 带格式时间转成不带格式.
     * 
     * @param str String
     * @return String
     * @author wuyx
     * 2011-5-12 wuyx 
     * 00:00:00->000000
     */
    public static String covertFormatTimeStrToStr(String str) {
        if (StrUtil.isNullOrEmpty(str) || str.length() != 8) {
            return "";
        }
        return str.replaceAll(":", "");
    }
    
    /**
     * 不带格式转成带格式时间.
     * 
     * @param str String
     * @return String
     * @author wuyx
     * 2011-5-12 wuyx 
     * 000000->00:00:00
     */
    public static String covertTimeStrToFormatStr(String str) {
        if (StrUtil.isNullOrEmpty(str) || str.length() != 6) {
            return "";
        }
        return str.scom.xhui.vimarket.zkplus+ ":" + str.substring(2, 4) + ":" + str.substring(4, 6);
    }
    
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */
    public void setAttrView(final Component comp, Map<String, Integer> attrViewMap) {
        if (attrViewMap != null && attrViewMap.size() > 0) {
            String attrJC = comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "";
            if (!StrUtil.isNullOrEmpty(attrJC)) {
                Integer viewType = attrViewMap.get(attrJC);
                // 取输入框
                final Timebox timebox = getTimebox(comp);
                if (timebox != null) {
                    timebox.setVisible(false);
                    if (AttrBaseComp.VIEW_TYPE_SHOW.equals(viewType)) {
                        timebox.setDisabled(true);
                        timebox.setVisible(true);
                    } else if (AttrBaseComp.VIEW_TYPE_EDIT.equals(viewType)) {
                      com.xhui.vimarket.zkplussible(true);
                    }
                }
            }
        }
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getMsgComp(org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-7-19 wuyx
     */
    @Override
    public Component getMsgComp(Component comp) {
        return getTimebox(comp);
    }
}
